import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';

// 登录相关组件
import Login from '../views/Login.vue'; // 登录页面
import Register from '../views/Register.vue'; // 注册页面

// 教师端组件
import Dashboard from '../views/teacher/Dashboard.vue'; // 教师控制台
import ExamRegistration from '../views/teacher/ExamRegistration.vue'; // 监考报名
import ExamRecords from '../views/teacher/ExamRecords.vue'; // 监考记录
import ExamBatchDetail from '../views/teacher/ExamBatchDetail.vue'; // 监考批次详情
import ExamSignup from '../views/teacher/ExamSignUp.vue'; // 监考报名表单
import Messages from '../views/teacher/MessagesView.vue'; // 消息中心
import ExamRules from '../views/teacher/ExamRules.vue'; // 监考准则

// 研工办主任端组件
import Dashboard2 from '../views/director/Dashboard2.vue'; // 研工办主任控制台
import ApprovalPending from '../views/director/ApprovalPending.vue'; // 待审批列表
import ApprovalSuccess from '../views/director/ApprovalSuccess.vue'; // 审批成功列表
import ApprovalRejected from '../views/director/ApprovalRejected.vue'; // 审批拒绝列表
import ExamList from '../views/director/ExamList.vue'; // 监考名单
import MaterialDetail from '../views/director/MaterialDetail.vue'; // 材料详情
import ApprovedList from '../views/director/ApprovedList.vue'; // 同意报名规则
import RejectedList from '../views/director/RejectedList.vue'; // 驳回规则
import DirectorMessages from '../views/director/Messages.vue'; // 研工办主任消息中心

// 考务科长端组件
import Dashboard3 from '../views/officer/Dashboard3.vue'; // 考务科长控制台主界面
import ApprovalDetail from '../views/officer/ApprovalDetail.vue'; // 报名审批详情查看页面
import ExamManagement from '../views/officer/ExamManagement.vue'; // 监考批次管理与发布
import ApplicantList from '../views/officer/ApplicantList.vue'; // 查看指定批次的报名人员名单
import ApprovalList from '../views/officer/ApprovalList.vue'; // 报名审批列表（同意/拒绝状态筛选）
import OfficerRejectedList from '../views/officer/RejectedList.vue'; // 被拒绝的报名名单管理
import CreateExamBatch from '../views/officer/ExamBatch.vue'; // 创建新监考批次
import ExamSchedule from '../views/officer/ExamSchedule.vue'; // 监考日程安排表管理
import ExamDetailList from '../views/officer/ExamDetailList.vue'; // 监考任务详细分配名单
import ExamConfirmationOverview from '../views/officer/ExamConfirmationOverview.vue'; // 监考通知确认总览
import ExamConfirmationDetail from '../views/officer/ExamConfirmationDetail.vue'; // 单场考试确认详情
import ExamPaymentOverview from '../views/officer/ExamPaymentOverview.vue'; // 监考费用结算总览
import ExamPaymentDetail from '../views/officer/ExamPaymentDetail.vue'; // 单场考试费用明细
import DepartmentManagement from '../views/officer/DepartmentManagement.vue'; // 部门与角色权限管理
import DepartmentMembers from '../views/officer/DepartmentMembers.vue'; // 部门成员管理
import Messages2 from '../views/officer/Messages.vue'; // 系统消息通知中心

// 学生端组件
import Dashboard4 from '../views/student/Dashboard4.vue'; // 学生控制台主界面 - 提供招生信息总览和快捷服务入口
import GraduateApplication from '../views/student/GraduateApplication.vue'; // 研究生报考流程 - 多步骤表单实现完整报考流程
import SchoolHistory from '../views/student/SchoolHistory.vue'; // 学校校史展示 - 时间线形式呈现学校发展历程
import FeedbackBox from '../views/student/FeedbackBox.vue'; // 意见反馈系统 - 支持提交建议和查看历史反馈


const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '登录 - 研究生监考报名系统',
            requiresAuth: false
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: {
            title: '注册 - 研究生监考报名系统',
            requiresAuth: false
        }
    },

    // 教师端路由
    {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
            title: '控制台 - 研究生监考报名系统',
            requiresAuth: true
        },
        children: [
            {
                path: 'exam-registration',
                name: 'ExamRegistration',
                component: ExamRegistration,
                meta: { title: '监考报名' }
            },
            {
                path: 'exam-records',
                name: 'ExamRecords',
                component: ExamRecords,
                meta: { title: '监考记录' }
            },
            {
                path: 'exam-rules',
                name: 'ExamRules',
                component: ExamRules,
                meta: { title: '监考准则' }
            },
            {
                path: 'messages',
                name: 'Messages',
                component: Messages,
                meta: { title: '消息中心' }
            },
            {
                path: 'exam-batch/:id',
                name: 'ExamBatchDetail',
                component: ExamBatchDetail,
                meta: {
                    title: '监考批次详情',
                    requiresAuth: true
                }
            },
            {
                path: 'exam-signup/:id',
                name: 'ExamSignup',
                component: ExamSignup,
                meta: {
                    title: '监考报名',
                    requiresAuth: true
                }
            },
            {
                path: 'material/:id',
                name: 'MaterialDetail',
                component: MaterialDetail,
                meta: { title: '材料详情' }
            }
        ]
    },

    // 研工办主任端路由
    {
        path: '/dashboard2',
        component: Dashboard2,
        meta: {
            requiresAuth: true,
            allowedRoles: ['director']
        },
        children: [
            {
                path: 'approval/pending',
                name: 'ApprovalPending',
                component: ApprovalPending,
                meta: { title: '报名等待审批' }
            },
            {
                path: 'approval/success',
                name: 'ApprovalSuccess',
                component: ApprovalSuccess,
                meta: { title: '报名成功列表' }
            },
            {
                path: 'approval/failed',
                name: 'ApprovalRejected',
                component: ApprovalRejected,
                meta: { title: '报名失败列表' }
            },
            {
                path: 'approved-list',
                name: 'ApprovedList',
                component: ApprovedList,
                meta: { title: '同意报名规则' }
            },
            {
                path: 'rejected-list',
                name: 'RejectedList',
                component: RejectedList,
                meta: { title: '驳回规则' }
            },
            {
                path: 'exam-list',
                name: 'DirectorExamList',
                component: ExamList,
                meta: { title: '监考名单' }
            },
            {
                path: 'messages',
                name: 'DirectorMessages',
                component: DirectorMessages,
                meta: { title: '消息中心' }
            },
            // 默认重定向
            {
                path: '',
                redirect: '/dashboard2/approval/pending'
            }
        ]
    },

    // 考务科长端路由
    {
        path: '/dashboard3',
        component: Dashboard3,
        meta: {
            requiresAuth: true,
            allowedRoles: ['officer'] // 仅允许考务科长访问
        },
        children: [
            // 报名审批列表路由
            {
                path: 'approval/:type(approved|rejected)', // 动态类型参数
                name: 'ApprovalList',
                component: ApprovalList,
                meta: { title: '报名审批' }
            },
            {
                path: 'approval/:type(approved|rejected)/:id', // 详情页
                name: 'ApprovalDetail',
                component: ApprovalDetail,
                meta: { title: '审批详情' }
            },
            {
                path: 'approval/approved',
                component: ApprovalList,
                meta: { title: '同意报名名单' }
            },
            {
                path: 'approval/rejected',
                component: OfficerRejectedList,
                meta: { title: '不同意报名名单' }
            },

            // 监考管理路由
            {
                path: 'exam-management',
                name: 'ExamManagement',
                component: ExamManagement,
                meta: { title: '监考管理' }
            },
            {
                path: 'create-batch',
                name: 'CreateExamBatch',
                component: CreateExamBatch,
                meta: { title: '创建监考批次' }
            },
            {
                path: 'exam-management/applicants/:batchId',
                name: 'ApplicantList',
                component: ApplicantList,
                meta: { title: '报名名单' }
            },
            {
                path: 'exam-schedule',
                name: 'ExamSchedule',
                component: ExamSchedule,
                meta: { title: '监考安排表' }
            },
            {
                path: 'exam-detail/:id',
                name: 'ExamDetailList',
                component: ExamDetailList,
                meta: { title: '监考详细名单' }
            },
            {
                path: 'exam-confirmation',
                name: 'ExamConfirmationOverview',
                component: ExamConfirmationOverview,
                meta: { title: '监考通知确认情况' }
            },
            {
                path: 'exam-confirmation/:id',
                name: 'ExamConfirmationDetail',
                component: ExamConfirmationDetail,
                meta: { title: '监考通知确认情况表' }
            },
            {
                path: 'exam-payment',
                name: 'ExamPaymentOverview',
                component: ExamPaymentOverview,
                meta: { title: '监考费用明细' }
            },
            {
                path: 'exam-payment/:id',
                name: 'ExamPaymentDetail',
                component: ExamPaymentDetail,
                meta: { title: '监考费用详细名单' }
            },
            {
                path: '/dashboard3/department-management',
                name: 'DepartmentManagement',
                component: DepartmentManagement,
                meta: { title: '部门与角色管理' }
            },
            {
                path: '/dashboard3/department-members',
                name: 'DepartmentMembers',
                component: DepartmentMembers,
                meta: { title: '成员名单' },
                props: route => ({
                    departmentId: route.query.departmentId,
                    departmentName: route.query.departmentName
                })
            },
            {
                path: 'messages',
                component: Messages2,
                meta: { title: '消息中心' }
            },

            // 默认重定向
            {
                path: '',
                redirect: '/dashboard3/approval/approved' // 默认打开同意报名列表
            }
        ]
    },

    //学生
    {
        path: '/dashboard4',
        component: Dashboard4,
        meta: {
            requiresAuth: true,
            allowedRoles: ['student'] // 仅允许考务科长访问
        },
        children: []
    },
    {
        path: '/application',
        name: 'GraduateApplication',
        component: GraduateApplication
    },
    {
        path: '/school-history',
        name: 'SchoolHistory',
        component: SchoolHistory,
        meta: {
            title: '学校校史 - 研究生招生系统',
            requiresAuth: true,
            allowedRoles: ['student']
        }
    },
    // 意见箱路由
    {
        path: '/feedback',
        name: 'FeedbackBox',
        component: FeedbackBox,
        meta: {
            title: '意见箱 - 研究生招生系统',
            requiresAuth: true,
            allowedRoles: ['student']
        }
    },



];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
});

router.beforeEach((to, from, next) => {
    const title = to.meta.title as string | undefined;
    document.title = title || '研究生监考报名系统';

    // 检查需要身份验证的路由
    if (to.meta.requiresAuth) {
        const userInfo = localStorage.getItem('userInfo');

        if (!userInfo) {
            next('/login');
            return;
        }

        // 检查角色权限
        const { role } = JSON.parse(userInfo);
        const allowedRoles = to.meta.allowedRoles || ['staff', 'director', 'officer'];

        if (!allowedRoles.includes(role)) {
            alert('您没有访问此页面的权限');
            next('/login');
            return;
        }
    }

    next();
});

export default router;